<template>
  <!--分配权限-->
  <div>
    <a-form :form="form" ref="grantForm">
    <!--  <f-form-item label="有效期" name="expireTime" type="date" :extra="getExtra" :value="formItem.expireTime"/>-->
      <f-form-item label="功能菜单" type="other" name="table">
        <vxe-table
          border
          resizable
          ref="tree"
          row-id="menuId"
          :tree-config="{children: 'children',expandAll: true}"
          :select-config="{labelField: 'menuName',checkRowKeys: checkRowKeys}"
          :data.sync="selectMenus">
          <vxe-table-column type="selection" field="menuId" title="菜单" tree-node width="220"></vxe-table-column>
          <vxe-table-column field="operation" title="操作">
            <template v-slot="{ row }">
              <a-checkbox-group v-model="formItem.grantActions">
                <a-checkbox :disabled="item.disabled" v-for="item in row.actionList" :value="item.authorityId">
                  <span :title="item.actionDesc">{{item.actionName}}</span>
                </a-checkbox>
              </a-checkbox-group>
            </template>
          </vxe-table-column>
        </vxe-table>
      </f-form-item>
    </a-form>

  </div>
</template>

<script>
  import XEUtils from 'xe-utils'
  import {
    getAuthorityMenu,
    getAuthorityRole
  } from '@/api/manage'

  export default {
    name: 'roleAuth',
    props: {},
    data() {
      return {
        form: this.$form.createForm(this),
        selectMenus: [],
        pageInfo: {},
        checkRowKeys: [],
        formItem: {
          roleId: '',
          grantActions: [],
          grantMenus: [],
          expireTime: this.$moment().format('YYYY-MM-DD'),
          isExpired: false
        }
      }
    },
    computed: {
      getExtra: function() {
        return this.formItem.isExpired ? '授权已过期' : ''
      }
    },
    methods: {
      /*初始数据*/
      setData(data) {
        if (data && data.roleId) {
          this.formItem.roleId = data.roleId
          this.handleLoadRoleGranted(data.roleId)
        }
      },
      /*获取已选择节点*/
      getCheckedAuthorities() {
        const menuIds = new Set()
        this.$refs['tree'].getSelectRecords().map(item => {
          menuIds.add(item.authorityId)
          menuIds.add(item.parentId)
        })
        return Array.from(menuIds).concat(this.formItem.grantActions)
      },
      /*提交授权*/
      handleSubmit(callback) {
        const authorityIds = this.getCheckedAuthorities()
        this.http.post(this, {
          url: this.api.authority.role,
          data: {
            roleId: this.formItem.roleId,
            expireTime: this.formItem.expireTime ? this.formItem.expireTime : '',
            authorityIds: authorityIds.join(',')
          },
          success: function(me) {
            callback()
          }
        })
      },
      /*加载权限*/
      handleLoadRoleGranted(roleId) {
        if (!roleId) {
          return
        }
        const that = this
        const p1 = getAuthorityMenu()
        const p2 = getAuthorityRole(roleId)
        Promise.all([p1, p2]).then(function(values) {
          let res1 = values[0]
          let res2 = values[1]
          if (res1.code === that.http.HTTP_OK_STATUS && res1.data) {
            if (res2.code === that.http.HTTP_OK_STATUS && res2.data && res2.data.length > 0) {
              let menus = []
              let actions = []
              res2.data.map(item => {
                // 菜单权限
                if (item.authority.indexOf('MENU_') != -1 && !menus.includes(item.authorityId)) {
                  menus.push(item.authorityId)
                }
                // 操作权限
                if (item.authority.indexOf('ACTION_') != -1 && !actions.includes(item.authorityId)) {
                  actions.push(item.authorityId)
                }
              })
              that.formItem.grantMenus = menus
              that.formItem.grantActions = actions
              // 时间
              if (res2.data.length > 0) {
                that.formItem.expireTime = res2.data[0].expireTime
                that.formItem.isExpired = res2.data[0].isExpired
              }
            }
            res1.data.map(item => {
              // 菜单选中
              if (that.formItem.grantMenus.includes(item.authorityId)) {
                if (item.parentId != 0 || item.path != '') {
                  //排除一级节点避免全部选中
                  that.checkRowKeys.push(item.menuId)
                }
              }
            })
            let opt = {
              key: 'menuId',
              parentKey: 'parentId',
              children: 'children'
            }
            that.selectMenus = XEUtils.toArrayTree(res1.data, opt)
          }
          that.modalVisible = true
        })
      }
    },
    created() {

    },
    components: {}
  }
</script>

<style scoped lang="less" rel="stylesheet/less">

</style>
